package com.demo.controller;

import com.demo.entity.AjaxResult;
import com.demo.entity.OrderLog;
import com.demo.entity.TbGoods;
import com.demo.service.TbGoodsService;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
public class GoodsController {

    @Autowired
    private TbGoodsService tbGoodsService;


    /**
     * 通过id查询商品信息
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/queryById")
    public AjaxResult<TbGoods> queryById(@RequestParam("goodsId") Long goodsId) {

        //断言给的对象notNull,如果为空就抛出一个非法参数异常，异常的提示信息我们指定的字串
        Assert.notNull(goodsId, "商品id不能为空");

        //google封装了参数校验
        //在guawa包下封装的
        //检查参数不能为空，为空的话就抛出来后面的提示信息
        Preconditions.checkNotNull(goodsId, "商品id不能为空");
        return tbGoodsService.findById(goodsId);
    }


    /**
     * 添加商品
     */
    @PostMapping("insert/goods")
    public AjaxResult insertGoods(@RequestBody TbGoods tbGoods){
        return tbGoodsService.insertGoods(tbGoods);
    }

    /**
     * 修改库存
     *
     * @param tbGoods
     * @return
     */
    @PostMapping("/updateStock")
    public AjaxResult updateStock(@RequestBody TbGoods tbGoods) {
        System.out.printf("进来啦 ===============================");
        Preconditions.checkArgument(Objects.nonNull(tbGoods.getGoodsId()), "商品ID不能为空");
        Preconditions.checkArgument(Objects.nonNull(tbGoods.getGoodsStock()), "商品库存数不能为空");
        return tbGoodsService.updateGoods(tbGoods);
    }

    /**
     * 添加日志
     * @return
     */
    @PostMapping("/save/log")
    public AjaxResult saveLog(@RequestBody OrderLog orderLog){
        return tbGoodsService.saveLog(orderLog);
    }


}
